﻿
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fd_Portada_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[fd_Portada_Update]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE fd_Portada_Update
(
	@portada	TEXT
)
AS


BEGIN

	BEGIN TRY
		SET DATEFORMAT ymd;
		DECLARE @XMLData XML
		
		SET @XMLData = CONVERT(XML, @portada)
		
		Update Portadas set
				   Asunto = (Select a.b.value('Asunto[1]','varchar(100)') AS Asunto FROM @XMLData.nodes('Portada') a(b)),
				   UnidadRemitente = (Select a.b.value('UnidadRemitente[1]','varchar(100)') AS UnidadRemitente FROM @XMLData.nodes('Portada') a(b)), 
				   FuncionarioRemitente = (Select a.b.value('FuncionarioRemitente[1]','varchar(100)') AS FuncionarioRemitente FROM @XMLData.nodes('Portada') a(b)),
				   CodPortada = (Select a.b.value('CodPortada[1]','varchar(50)') AS CodPortada FROM @XMLData.nodes('Portada') a(b)),
				   CodDocumento = (Select a.b.value('CodDocumento[1]','varchar(50)') AS CodDocumento FROM @XMLData.nodes('Portada') a(b)),
				   Fecha = (SELECT a.b.value('Fecha[1]', 'datetime')  AS Fecha FROM @XMLData.nodes('Portada') a(b))
		from Portadas where id = 
			(SELECT a.b.value('id[1]','int') AS id FROM @XMLData.nodes('Portada') a(b))
		
	END TRY
   	
   	BEGIN CATCH
   		DECLARE 	@ErrorNumber	INT,
					@ErrorSeverity	INT,
					@ErrorState		INT,
					@ErrorProcedure NVARCHAR(126),
					@ErrorLine		INT,
					@ErrorMessage	NVARCHAR(4000)
				
   		SELECT		@ErrorNumber = ERROR_NUMBER(),
					@ErrorSeverity = ERROR_SEVERITY(),
					@ErrorState = ERROR_STATE(),
					@ErrorProcedure = ERROR_PROCEDURE(),
					@ErrorLine = ERROR_LINE(),
					@ErrorMessage = ERROR_MESSAGE()
				
   		RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState)
   	END CATCH

END


